package com.immunology.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.immunology.model.MappingPages;
import com.immunology.model.Patient;
import com.immunology.service.PatientService;

public class SearchPatientServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public SearchPatientServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String textsearch = request.getParameter("searchText");
		PatientService patientService = new PatientService();
		List<Patient> patients = null;
		try {
			patients = patientService.searchPatient(textsearch);
		} catch (SQLException e) {
			System.out.println("Can't get data from patient table!");
			e.printStackTrace();
		}
		request.setAttribute("patients", patients);
		request.getRequestDispatcher(MappingPages.SEARCH_PATIENT_JSP).forward(
				request, response);
	}

}
